package com.gxa.autoparts.admin.service.impl;

import com.gxa.autoparts.admin.mapper.ChartMapper;
import com.gxa.autoparts.admin.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Bei
 * @date 2022/1/6 17:47
 */
@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    ChartMapper chartMapper;

    /**
     * 日销量某个时间段的数据
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Map orderByDay(String startTime,String endTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse(startTime);
        Date parse1 = simpleDateFormat.parse(endTime);
        Map<String,Object> map = new LinkedHashMap<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(parse1);
        calendar1.set(Calendar.HOUR,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.SECOND,0);
        //循环出上个月最后一天到今天的数据
        while (calendar1.getTime().compareTo(parse) > 0){
            System.out.println("标记1");
            //终止时间
            String format1 = simpleDateFormat.format(calendar1.getTime());
            calendar1.add(Calendar.DATE,-1);

            //开始时间
            String format2 = simpleDateFormat.format(calendar1.getTime());
            Integer integer = chartMapper.orderCount(format2, format1);
            map.put(format2,integer);
        }
        return map;
    }


    /**
     * 日销量最近一月数据
     * @return
     * @throws ParseException
     */
    @Override
    public Map orderByLastMonth(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,Object> map = new LinkedHashMap<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(Calendar.HOUR,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.SECOND,0);
        //循环出上个月最后一天到今天的数据
        int i = calendar1.get(Calendar.DAY_OF_MONTH);
        while (i > 0){
            //终止时间
            String format1 = simpleDateFormat.format(calendar1.getTime());
            calendar1.add(Calendar.DATE,-1);

            //开始时间
            String format2 = simpleDateFormat.format(calendar1.getTime());
            Integer integer = chartMapper.orderCount(format2, format1);
            map.put(format2,integer);
            i--;
        }
        return map;
    }

    /**
     * 订单项饼图未选择时间的接口
     */
    @Override
    public Map orderItemByLastMonth(String startTime,String endTime){
        List<Map> maps = chartMapper.orderItemByLastMonth(startTime,endTime);
        Map<String,Object> map = new HashMap<>();
        for (Map map1 : maps) {
            Object name = map1.get("name");
            Object saleCount = map1.get("saleCount");
            map.put(name.toString(),saleCount);

        }
        return map;
    }

    @Override
    public Map userReg(String startTime,String endTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse(startTime);
        Date parse1 = simpleDateFormat.parse(endTime);
        Map<String,Object> map = new LinkedHashMap<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(parse1);
        calendar1.set(Calendar.HOUR,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.SECOND,0);
        //循环出上个月最后一天到今天的数据
        while (calendar1.getTime().compareTo(parse) > 0){
            System.out.println("标记1");
            //终止时间
            String format1 = simpleDateFormat.format(calendar1.getTime());
            calendar1.add(Calendar.DATE,-1);

            //开始时间
            String format2 = simpleDateFormat.format(calendar1.getTime());
            Integer integer = chartMapper.userOrderCount(format2, format1);
            map.put(format2,integer);
        }
        return map;
    }

    @Override
    public Map userRegLastMonth() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,Object> map = new LinkedHashMap<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(Calendar.HOUR,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.SECOND,0);
        //循环出上个月最后一天到今天的数据
        int i = calendar1.get(Calendar.DAY_OF_MONTH);
        while (i > 0){
            //终止时间
            String format1 = simpleDateFormat.format(calendar1.getTime());
            calendar1.add(Calendar.DATE,-1);

            //开始时间
            String format2 = simpleDateFormat.format(calendar1.getTime());
            Integer integer = chartMapper.userOrderCount(format2, format1);
            map.put(format2,integer);
            i--;
        }
        return map;
    }

    @Override
    public Map countPeople(String startTime, String endTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse(startTime);
        Date parse1 = simpleDateFormat.parse(endTime);
        Map<String,Object> map = new LinkedHashMap<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(parse1);
        calendar1.set(Calendar.HOUR,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.SECOND,0);
        //循环出上个月最后一天到今天的数据
        while (calendar1.getTime().compareTo(parse) > 0){
            System.out.println("标记1");
            //终止时间
            String format1 = simpleDateFormat.format(calendar1.getTime());
            calendar1.add(Calendar.DATE,-1);

            //开始时间
            String format2 = simpleDateFormat.format(calendar1.getTime());
            Integer integer = chartMapper.peopleCount(format2, format1);
            map.put(format2,integer);
        }
        return map;
    }

    @Override
    public Map countPeopleByLastMonth(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,Object> map = new LinkedHashMap<>();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(Calendar.HOUR,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.SECOND,0);
        //循环出上个月最后一天到今天的数据
        int i = calendar1.get(Calendar.DAY_OF_MONTH);
        while (i > 0){
            //终止时间
            String format1 = simpleDateFormat.format(calendar1.getTime());
            calendar1.add(Calendar.DATE,-1);

            //开始时间
            String format2 = simpleDateFormat.format(calendar1.getTime());
            Integer integer = chartMapper.peopleCount(format2, format1);
            map.put(format2,integer);
            i--;
        }
        return map;
    }

}
